//
//  CYYYSZDDetailVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/11/21.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYYYSZDDetailVC.h"
#import "CYUser.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYDES3Util.h"
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import <ZMCreditSDK/ALCreditService.h>
#import "DCPaymentView.h"

@interface CYYYSZDDetailVC ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *fwPasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *dealCodeTF;
@property (nonatomic, strong)UIWebView *webview;
@property (weak, nonatomic) IBOutlet UIButton *tongdao1Btn;
@property (weak, nonatomic) IBOutlet UIButton *tongdao2Btn;
@property (nonatomic, strong) NSString *selectstr;
@property (weak, nonatomic) IBOutlet UIButton *tongdao3Btn;

@end

@implementation CYYYSZDDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor =CYColor(114, 197, 83, 1);
     _fwPasswordTF.secureTextEntry = YES;
     _dealCodeTF.secureTextEntry = YES;
     _selectstr = @"1";
     [_tongdao1Btn setBackgroundImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];

    // Do any additional setup after loading the view from its nib.
}

- (IBAction)tongdao1Click:(id)sender {
     _selectstr =@"1";
     [_tongdao1Btn setBackgroundImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
     [_tongdao2Btn setBackgroundImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     
}
- (IBAction)tongdao2Click:(id)sender {
     _selectstr = @"2";
     [_tongdao1Btn setBackgroundImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_tongdao2Btn setBackgroundImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
}

- (IBAction)tongdao3Onclick:(id)sender {
     _selectstr = @"3";
     [_tongdao1Btn setBackgroundImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_tongdao2Btn setBackgroundImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_tongdao3Btn setBackgroundImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
}

- (IBAction)submitClick:(id)sender {
     //通道1 apix
     //通道2 juhe
     
     if (_fwPasswordTF.text.length == 0) {
          CYAlertView(@"请输入手机服务密码");
          return;
     }
     if (_dealCodeTF.text.length == 0) {
          CYAlertView(@"请输入交易密码");
          return;
     }
     NSString *desstr = [CYDES3Util encrypt:_dealCodeTF.text];

     CYUser *user = [CYLoginHttpTool userData].user;
     _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
     _webview.backgroundColor = CYColor(114, 197, 83, 1);
     _webview.delegate = self;

     if (user) {
          [[self navigationController] setNavigationBarHidden:YES animated:NO];
          //http://192.168.0.172:8058   http://www.credit-manage.com:8058
          
          //phone/apix/saveApixMnoAccountFirst.htm
          
          if (_selectstr.intValue == 1) {
               NSString *urlstr = [NSString stringWithFormat:@"http://www.credit-manage.com:8058/phone/apix/saveApixMnoAccountFirst.htm?userId=%@&token=%@&password=%@&dealCode=%@",user.userId,user.token,_fwPasswordTF.text,desstr];
               NSString *strUrl = [urlstr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
               NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
               [_webview loadRequest:request];
               [self.view addSubview:_webview];
               
          }else if (_selectstr.intValue == 2){
               NSString *urlstr = [NSString stringWithFormat:@"http://www.credit-manage.com:8058/phone/juhe/saveJuHeMnoAccount.htm?userId=%@&token=%@&password=%@&dealCode=%@",user.userId,user.token,_fwPasswordTF.text,desstr];
               NSString *strUrl = [urlstr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
               NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
               [_webview loadRequest:request];
               [self.view addSubview:_webview];

          }else if (_selectstr.intValue == 3) {
//               http://www.credit-manage.com:8058/phone/user/checkCertificateNoAndBandCard.htm?userId=%@&token=%@&password=%@&dealCode=%@
               NSString *urlstr = [NSString stringWithFormat:@"http://www.credit-manage.com:8058/phone/juhe/saveSystemMno.htm?userId=%@&token=%@&password=%@&dealCode=%@",user.userId,user.token,_fwPasswordTF.text,desstr];
               NSString *strUrl = [urlstr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
               NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
               [_webview loadRequest:request];
               [self.view addSubview:_webview];
          }
   }

     
    
}

#pragma mark -
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
          if (buttonIndex == 0) {
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
     }];
     
}

//强壮 铁壁4921 幸运7400 抗拒3743 招架3738 飞行4841 固发22624
//365830896    liuchang324   15166136623


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     //[self.view addSubview:_hiview];
     //[MBProgressHUD hideHUDForView:self.view];
     NSString *url = [[request URL] absoluteString];
     NSLog(@"url = %@",url);
     //h5内返回控件字段方法:loanlist
     if([url hasSuffix:@"loanlist"]) {
          dispatch_async(dispatch_get_main_queue(), ^{
               //返回上一级时，取消nav的隐藏
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               [self dismissViewControllerAnimated:YES completion:^{
                    //do what you want
                    
               }];
               //js交互oc获取到h5内的返回按钮后，点击返回，popvc
               [self.navigationController popViewControllerAnimated:YES];
          });
          return NO;
     }
     
     return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
